package cn.magicduck.demo.factory;

import cn.magicduck.demo.factory.ICacheAdapter;
import cn.magicduck.demo.util.ClassLoaderUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @projectName: design-pattern-demo
 * @className: JDKInvocationHandler
 * @description: 无须描述
 * @author: MagicDuck
 * @create: 2020-07-17 13:57
 **/
public class JDKInvocationHandler implements InvocationHandler {

    private ICacheAdapter cacheAdapter;

    public JDKInvocationHandler(ICacheAdapter cacheAdapter) {
        this.cacheAdapter = cacheAdapter;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        return ICacheAdapter.class.getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args)).invoke(cacheAdapter, args);
    }
}
